package com.ruoyi.zhgd.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.zhgd.domain.bo.WechatWarnMsgBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zhgd.domain.vo.ZjNWarningHntVo;
import com.ruoyi.zhgd.domain.bo.ZjNWarningHntBo;
import com.ruoyi.zhgd.service.IZjNWarningHntService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自建混凝土拌合站报警管理
 *
 * @author zyb
 * @date 2023-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhgd/nWarningHnt")
public class ZjNWarningHntController extends BaseController {

    private final IZjNWarningHntService iZjNWarningHntService;

    /**
     * 查询自建混凝土拌合站报警管理列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<ZjNWarningHntVo> list(ZjNWarningHntBo bo, PageQuery pageQuery) {
        return iZjNWarningHntService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出自建混凝土拌合站报警管理列表
     */
    @SaCheckLogin
    @Log(title = "自建混凝土拌合站报警管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZjNWarningHntBo bo, HttpServletResponse response) {
        List<ZjNWarningHntVo> list = iZjNWarningHntService.queryList(bo);
        ExcelUtil.exportExcel(list, "自建混凝土拌合站报警管理", ZjNWarningHntVo.class, response);
    }

    /**
     * 获取自建混凝土拌合站报警管理详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<ZjNWarningHntVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable String id) {
        return R.ok(iZjNWarningHntService.queryById(id));
    }

    /**
     * 新增自建混凝土拌合站报警管理
     */
    @SaCheckLogin
    @Log(title = "自建混凝土拌合站报警管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZjNWarningHntBo bo) {
        return toAjax(iZjNWarningHntService.insertByBo(bo));
    }

    /**
     * 修改自建混凝土拌合站报警管理
     */
    @SaCheckLogin
    @Log(title = "自建混凝土拌合站报警管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZjNWarningHntBo bo) {
        return toAjax(iZjNWarningHntService.updateByBo(bo));
    }

    /**
     * 删除自建混凝土拌合站报警管理
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "自建混凝土拌合站报警管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iZjNWarningHntService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/ListDayStatistics")
    public R ListDayStatistics(@RequestParam Map<String,Object> params){
        return R.ok(iZjNWarningHntService.selectDayStatistics(params));
    }


    /**
     * 新增自建混凝土拌合站报警管理
     */
//    @SaCheckLogin
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/SendWXOAMessage")
    public R<Void> wechatSendMessage(@Validated @RequestBody WechatWarnMsgBo bo) {
        String body = HttpRequest.post("http://jsgl.sdgsbim.com:9081/api/Assignment/wxmessageservice/SendWXOAMessage")
            .timeout(8000)
            .header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")
            .body(JSONUtil.toJsonStr(bo))
            .execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        // 返回结果不正常时
        if (jsonObject.getInt("code") != 200) {
            throw new ServiceException("数字化平台公众号消息推送相关信息接口异常");
        }
        return R.warn(jsonObject.getJSONObject("data").getStr("msg"));
    }
}
